﻿using Device.Domain;
using Device.Infrstructre.Interface;
using Device.Read.API.Applcation.Command.UnitCommand;
using Device.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Device.Read.API.Controllers
{
    /// <summary>
    /// 单位显示控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class UnitReadController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public UnitReadController(IMediator mediator)
        {
            this._mediator = mediator;
        }
        /// <summary>
        /// 显示单位
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回受影响的实体</returns>
        [HttpPost]
        public async Task<ApiResult<Unitpage<UnitModel>>> SelectUnitModel(SelectUnitCommand command)
        {
            ApiResult<Unitpage<UnitModel>> result = new ApiResult<Unitpage<UnitModel>>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填单位
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<UnitModel>> GetUnitById([FromQuery] UnitFanCommand command)
        {
            ApiResult<UnitModel> result = new ApiResult<UnitModel>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 单位信息表导出
        /// </summary>
        /// <param name="command">要导出的信息</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<FileContentResult> GetAllUnitExel([FromQuery] SelectUnitCommand command)
        {
            var obj = await _mediator.Send(command);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("单位信息表");
            IRow row;
            row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("单位编号");
            row.CreateCell(1).SetCellValue("单位名称");
            row.CreateCell(2).SetCellValue("备注");
            int index = 1;
            foreach (var item in obj.list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.UnitCode);
                row.CreateCell(1).SetCellValue(item.UnitName);
                row.CreateCell(2).SetCellValue(item.Uremark);
                index++;
            }
            byte[] buffe;
            using(MemoryStream ms=new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dslist = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dslist, "单位信息表.xlsx");
        }
    }
}
